.TH std::integer_sequence 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::integer_sequence \- std::integer_sequence

.SH Synopsis
   Defined in header <utility>
   template< class T, T... Ints >  \fI(since C++14)\fP
   class integer_sequence;

   The class template std::integer_sequence represents a compile-time sequence of
   integers. When used as an argument to a function template, the parameter pack Ints
   can be deduced and used in pack expansion.

.SH Template parameters

   T       - an integer type to use for the elements of the sequence
   ...Ints - a non-type parameter pack representing the sequence

.SH Member types

   Member type Definition
   value_type  T

.SH Member functions

   size     returns the number of elements in Ints
   \fB[static]\fP \fI(public static member function)\fP

std::integer_sequence::size

   static constexpr std::size_t size() noexcept;

   Returns the number of elements in Ints. Equivalent to sizeof...(Ints).

.SH Parameters

   \fI(none)\fP

.SH Return value

   The number of elements in Ints.

   Helper templates

   A helper alias template std::index_sequence is defined for the common case where T
   is std::size_t:

   template< std::size_t... Ints >
   using index_sequence = std::integer_sequence<std::size_t, Ints...>;

   Helper alias templates std::make_integer_sequence and std::make_index_sequence are
   defined to simplify creation of std::integer_sequence and std::index_sequence types,
   respectively, with 0, 1, 2, ..., N - 1 as Ints:

   template< class T, T N >
   using make_integer_sequence = std::integer_sequence<T, /* a sequence 0, 1, 2, ...,
   N-1 */>;
   template< std::size_t N >
   using make_index_sequence = std::make_integer_sequence<std::size_t, N>;

   The program is ill-formed if N is negative. If N is zero, the indicated type is
   integer_sequence<T>.

   A helper alias template std::index_sequence_for is defined to convert any type
   parameter pack into an index sequence of the same length:

   template< class... T >
   using index_sequence_for = std::make_index_sequence<sizeof...(T)>;

.SH Notes

       Feature-test macro      Value    Std              Feature
   __cpp_lib_integer_sequence 201304L \fI(C++14)\fP Compile-time integer sequences

.SH Example

   Note: see Possible Implementation in std::apply for another example.


// Run this code

 #include <array>
 #include <cstddef>
 #include <iostream>
 #include <tuple>
 #include <utility>

 // debugging aid
 template<typename T, T... ints>
 void print_sequence(std::integer_sequence<T, ints...> int_seq)
 {
     std::cout << "The sequence of size " << int_seq.size() << ": ";
     ((std::cout << ints << ' '), ...);
     std::cout << '\\n';
 }

 // convert array into a tuple
 template<typename Array, std::size_t... I>
 auto a2t_impl(const Array& a, std::index_sequence<I...>)
 {
     return std::make_tuple(a[I]...);
 }

 template<typename T, std::size_t N, typename Indices = std::make_index_sequence<N>>
 auto a2t(const std::array<T, N>& a)
 {
     return a2t_impl(a, Indices{});
 }

 // pretty-print a tuple
 template<class Ch, class Tr, class Tuple, std::size_t... Is>
 void print_tuple_impl(std::basic_ostream<Ch, Tr>& os,
                       const Tuple& t,
                       std::index_sequence<Is...>)
 {
     ((os << (Is == 0? "" : ", ") << std::get<Is>(t)), ...);
 }

 template<class Ch, class Tr, class... Args>
 auto& operator<<(std::basic_ostream<Ch, Tr>& os,
                  const std::tuple<Args...>& t)
 {
     os << "(";
     print_tuple_impl(os, t, std::index_sequence_for<Args...>{});
     return os << ")";
 }

 int main()
 {
     print_sequence(std::integer_sequence<unsigned, 9, 2, 5, 1, 9, 1, 6>{});
     print_sequence(std::make_integer_sequence<int, 20>{});
     print_sequence(std::make_index_sequence<10>{});
     print_sequence(std::index_sequence_for<float, std::iostream, char>{});

     std::array<int, 4> array = {1, 2, 3, 4};

     // convert an array into a tuple
     auto tuple = a2t(array);
     static_assert(std::is_same_v<decltype(tuple),
                                  std::tuple<int, int, int, int>>, "");

     // print it to cout
     std::cout << "The tuple: " << tuple << '\\n';
 }

.SH Output:

 The sequence of size 7: 9 2 5 1 9 1 6
 The sequence of size 20: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 The sequence of size 10: 0 1 2 3 4 5 6 7 8 9
 The sequence of size 3: 0 1 2
 The tuple: (1, 2, 3, 4)

.SH See also

   to_array          creates a std::array object from a built-in array
   (C++20)           \fI(function template)\fP
   integral_constant
   bool_constant     compile-time constant of specified type with specified value
   \fI(C++11)\fP           \fI(class template)\fP
   \fI(C++17)\fP
